﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.SaleOrderInfoCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.SaleOrderInfoCommandHandler
{
    public class AddSaleOrderInfoCommandHandler : IRequestHandler<AddSaleOrderInfoCommand, APIResult<object>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<SaleOrderInfo> orderBiz;
        private readonly IBaseRepository<OrderMaterials> materialBiz;

        public AddSaleOrderInfoCommandHandler(IMapper mapper, IBaseRepository<SaleOrderInfo> orderBiz, IBaseRepository<OrderMaterials> materialBiz)
        {
            this.mapper = mapper;
            this.orderBiz = orderBiz;
            this.materialBiz = materialBiz;
        }

        public Task<APIResult<object>> Handle(AddSaleOrderInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            SaleOrderInfo saleOrderInfo = new SaleOrderInfo();
            saleOrderInfo.Id = Guid.NewGuid();
            saleOrderInfo.Address = request.Address;
            saleOrderInfo.CustomerId = request.CustomerId;
            saleOrderInfo.Oprator = request.Oprator;
            saleOrderInfo.Remark = request.Remark;
            saleOrderInfo.SaleUser = request.SaleUser;
            saleOrderInfo.BusinessStatus = request.BusinessStatus;  
            saleOrderInfo.DeliverDate = request.DeliverDate;
            saleOrderInfo.BusinessType = request.BusinessType;
            saleOrderInfo.DocumentDate = request.DocumentDate;
            saleOrderInfo.ExamUser = request.ExamUser;
            saleOrderInfo.Contract = request.Contract;
            saleOrderInfo.DeliveryStatus = request.DeliveryStatus;
            saleOrderInfo.ExamDate = request.ExamDate;
            saleOrderInfo.DocumentType = request.DocumentType;
            saleOrderInfo.Phone = request.Phone;
            saleOrderInfo.PayDate = request.PayDate;
            saleOrderInfo.DocumentStatus = request.DocumentStatus;
            saleOrderInfo.GoalCountry = request.GoalCountry;
            saleOrderInfo.SaleType = request.SaleType;
            saleOrderInfo.Address = request.Address;
            saleOrderInfo.Oprator = request.Oprator;
            saleOrderInfo.Receiver = request.Receiver;
            int add1 = orderBiz.Add(saleOrderInfo).Result;
            if(add1 > 0)
            {  
                request.OrderMaterials.ForEach(c =>
                {
                    c.SaleCode = saleOrderInfo.Id;
                });
                int add2 = materialBiz.AddRanges(request.OrderMaterials).Result;
                if (add2 > 0)
                {
                    result.Code = APIEnum.添加成功;
                    result.Msg = APIEnum.添加成功.ToString();
                }
                else
                {
                    result.Code = APIEnum.添加失败;
                    result.Msg = APIEnum.添加失败.ToString();
                }
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
            }
            return Task.FromResult(result);
        }
    }
}
